Skip to content

feat: importable getFormContext#269

Merged
alexluckett merged 14 commits intomainfrom
feat/importable-form-context
Dec 12, 2025
Merged

feat: importable getFormContext#269
alexluckett merged 14 commits intomainfrom
feat/importable-form-context

Conversation

@davidkelley
Copy link
Copy Markdown
Contributor

@davidkelley davidkelley commented Nov 28, 2025

Refactored the form context helper so it now shares the same model-loading path as the core routes: resolveFormModel builds/reuses cached models with correct base paths (including preview/prefix handling), email checks, and version metadata. The external getFormContext now pulls cached state, keeps reference numbers, and returns contexts via the shared model. Exposed the helper exports through the plugin entrypoint for consumers. Tests were updated to use typed mocks and tidy import ordering. Core runtime behavior stays the same; we just removed duplication and made the helper safe to consume outside the journey.

Copilot AI review requested due to automatic review settings November 28, 2025 13:33
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extracts form model resolution and context retrieval logic into a new reusable module (form-context.ts), making key functions like getFormContext, getFormModel, and related utilities importable for use outside the route handlers.

Key Changes

  • Created new form-context.ts module with form model resolution, caching, and context retrieval logic
  • Refactored routes/index.ts to use the new resolveFormModel function, reducing duplication
  • Exported form context utilities from the main engine plugin index for public API use

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 9 comments.

File Description
src/server/plugins/engine/form-context.ts New module containing form model resolution, caching logic, and helper functions for form context and answer mapping
src/server/plugins/engine/form-context.test.ts Comprehensive test suite for the new form-context module covering all exported functions
src/server/plugins/engine/routes/index.ts Refactored to use resolveFormModel function, removing ~70 lines of duplicated code
src/server/plugins/engine/index.ts Added exports for getFormContext, getFormModel, getFirstJourneyPage, mapFormContextToAnswers, and resolveFormModel

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/server/plugins/engine/form-context.ts Outdated
Comment thread src/server/plugins/engine/beta/form-context.ts
Comment thread src/server/plugins/engine/form-context.ts Outdated
Comment thread src/server/plugins/engine/form-context.ts
Comment thread src/server/plugins/engine/form-context.ts Outdated
Comment thread src/server/plugins/engine/beta/form-context.ts
Comment thread src/server/plugins/engine/form-context.ts
Comment thread src/server/plugins/engine/beta/form-context.ts
Comment thread src/server/plugins/engine/index.ts Outdated
Comment thread src/server/plugins/engine/form-context.ts Outdated
@alexluckett alexluckett self-requested a review December 3, 2025 14:00
Copy link
Copy Markdown
Contributor

@alexluckett alexluckett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, @davidkelley! Few questions.

Comment thread src/server/plugins/engine/routes/index.ts
Comment on lines +96 to +114
const summaryRequest: SummaryRequest = {
app: {},
method: 'get',
params: {
path: 'summary',
slug: journey,
...(isPreviewState(state, options) && {
state: resolveState(state)
})
},
path: `/${formModel.basePath}/summary`,
query: {},
url: new URL(
`/${formModel.basePath}/summary`,
'https://form-context.local'
),
server,
yar
}
Copy link
Copy Markdown
Contributor

@alexluckett alexluckett Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just trying to think into our chat a few weeks ago, hoping you can refresh my memory...

Are we mocking out this request to speed up delivery of the feature and unblock everyone, with a view that this will be uplifted later on to do it the 'right' way and avoid the request being passed too further down into the hierarchy?

Did you/your team look into how feasible that would be? If so, did you have an idea of effort/time required?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @alexluckett , unfortunately we ran out of time to do so really.. we're a bit behind on the migration

Comment on lines +88 to +92
journey: string,
state: JourneyState = FormStatus.Live,
options: FormContextOptions = {}
): Promise<FormContext> {
const formModel = await resolveFormModel(server, journey, state, options)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we keep to consistent slug terminology with the rest of the codebase please? journey isn't a term we use.

Comment thread src/server/plugins/engine/beta/form-context.ts
Comment thread src/server/plugins/engine/form-context.ts Outdated

const formState = {
...cachedState,
$$__referenceNumber: cachedState.$$__referenceNumber ?? 'TODO'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove 'TODO and just leave it as undefined? Presumably engine generates one if it's empty anyway?

@sonarqubecloud
Copy link
Copy Markdown

@alexluckett alexluckett merged commit 3174b0a into main Dec 12, 2025
23 checks passed
@alexluckett alexluckett deleted the feat/importable-form-context branch December 12, 2025 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants